package cn.edu.jxau.core.web;

import cn.edu.jxau.core.util.CastUtils;

import java.util.*;

/**
 * Desc:
 * ------------------------------------
 * Author:fulei04@meituan.com
 * Date:2017/12/17
 * Time:下午2:38
 */
public class Param {

    /**
     * 表单域参数
     */
    private List<FormFieldParam> formFieldParamList;
    private Map<String, Object> formFieldMap;

    /**
     * 文件参数
     */
    private List<FileFieldParam> fileFieldParamList;
    private Map<String, List<FileFieldParam>> fileFieldParamMap;

    public Param(List<FormFieldParam> formFieldParamList) {
        this(formFieldParamList, null);
    }

    public Param(List<FormFieldParam> formFieldParamList, List<FileFieldParam> fileFieldParamList) {

        this.formFieldParamList = formFieldParamList;
        formFieldMap = getFormFieldMap();
        this.fileFieldParamList = fileFieldParamList;
        fileFieldParamMap = getFileFieldParamMap();
    }

    private Map<String, Object> getFormFieldMap() {

        if (formFieldParamList == null || formFieldParamList.isEmpty()) {
            return Collections.EMPTY_MAP;
        }
        Map<String, Object> fieldMap = new HashMap<>();
        for (FormFieldParam formFieldParam : formFieldParamList) {
            fieldMap.put(formFieldParam.getName(), formFieldParam.getValue());
        }
        return fieldMap;
    }

    private Map<String, List<FileFieldParam>> getFileFieldParamMap() {

        if (fileFieldParamList == null || fileFieldParamList.isEmpty()) {
            return Collections.EMPTY_MAP;
        }
        Map<String, List<FileFieldParam>> fileParamMap = new HashMap<>();
        for (FileFieldParam fileFieldParam : fileFieldParamList) {
            String fieldName = fileFieldParam.getFieldName();
            if (fileParamMap.containsKey(fieldName)) {
                fileParamMap.get(fieldName).add(fileFieldParam);
            } else {
                List<FileFieldParam> list = new ArrayList<>();
                list.add(fileFieldParam);
                fileParamMap.put(fieldName, list);
            }
        }
        return fileParamMap;
    }

    public List<FileFieldParam> getFileFieldParamList(String fieldName) {
        return fileFieldParamMap.get(fieldName);
    }

    public FileFieldParam getFileFieldParam(String fieldName) {

        List<FileFieldParam> list = fileFieldParamMap.get(fieldName);
        return list == null ? null : list.get(0);
    }

    public boolean isEmpty() {

        if (formFieldParamList != null && !formFieldParamList.isEmpty()) {
            return false;
        }
        if (fileFieldParamList != null && !fileFieldParamList.isEmpty()) {
            return false;
        }
        return true;
    }

    public String getString(String fieldName) {
        return CastUtils.cast2String(formFieldMap.get(fieldName),null);
    }
    public long getLong(String fieldName) {
        return CastUtils.cast2Long(formFieldMap.get(fieldName),null);
    }
    public double gerDouble(String fieldName) {
        return CastUtils.cast2Double(formFieldMap.get(fieldName),null);
    }
    public boolean getBoolean(String fieldName) {
        return CastUtils.cast2Boolean(formFieldMap.get(fieldName),null);
    }
}
